package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.widget.LinearLayout
import android.widget.RelativeLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.BankListBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import kotlinx.android.synthetic.main.activity_bank_aty.*

class BankAty : BaseAty() {
    private lateinit var adp:BaseQuickAdapter<BankListBean.DataBean,BaseViewHolder>
    private val datas=ArrayList<BankListBean.DataBean>()
    override fun getContentId(): Int {
        return R.layout.activity_bank_aty
    }
    override fun initView() {
        adp=object :BaseQuickAdapter<BankListBean.DataBean,BaseViewHolder>(R.layout.bank_item,datas){
            override fun convert(helper: BaseViewHolder?, item: BankListBean.DataBean?) {
                val loBack = helper?.getView<RelativeLayout>(R.id.loBack)
                helper?.setText(R.id.itemTittle,item?.bank_name)
                val str = item?.id_card
                val num = str?.substring(str?.length - 4, str?.length )
                helper?.setText(R.id.itemNum,num)
                if (item?.bank_name?.contains("农")!!){
                    helper?.setImageResource(R.id.imgType,R.mipmap.ic_tuiguang_nh)
                    loBack?.setBackgroundResource(R.mipmap.tuiguang_nhbj)
                }else if (item?.bank_name?.equals("中国银行")!!){
                    helper?.setImageResource(R.id.imgType,R.mipmap.ic_tuiguang_zh)
                    loBack?.setBackgroundResource(R.mipmap.tuiguang_zhbj)
                }else if (item?.bank_name.contains("建")){
                    helper?.setImageResource(R.id.imgType,R.mipmap.ic_tuiguang_jh)
                    loBack?.setBackgroundResource(R.mipmap.tuiguang_jhbj)
                }else if (item?.bank_name.contains("工")){
                    helper?.setImageResource(R.id.imgType,R.mipmap.ic_tuiguang_gh)
                    loBack?.setBackgroundResource(R.mipmap.tuiguang_ghbj)
                }
            }
        }
        listView.adapter=adp
        listView.layoutManager=LinearLayoutManager(this,LinearLayout.VERTICAL,false)
        OkGo.post<BankListBean>(Urls.BaseUrl+Urls.BANK)
                .execute(object :JsonCallBack<BankListBean>(this){
                    override fun onSuccess(response: Response<BankListBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            datas.clear()
                            datas.addAll(bean?.data)
                            adp.notifyDataSetChanged()
                        }
                    }
                })


    }

    override fun onResume() {
        super.onResume()
        OkGo.post<BankListBean>(Urls.BaseUrl+Urls.BANK)
                .execute(object :JsonCallBack<BankListBean>(this){
                    override fun onSuccess(response: Response<BankListBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            datas.clear()
                            datas.addAll(bean?.data)
                            adp.notifyDataSetChanged()
                        }
                    }
                })
    }
    override fun initListener() {
        btExit.setOnClickListener {
            finish()
        }
        btAdd.setOnClickListener {
            startActivity(Intent(this,AddBankAty::class.java))
        }
        adp.setOnItemClickListener { adapter, view, position ->
            val data = Intent()
            data.putExtra("id",datas[position].id.toString())
            data.putExtra("name",datas[position].bank_name.toString()+"("+(datas[position].id_card?.substring(datas[position].id_card?.length!! - 4, datas[position].id_card?.length!!) ).toString()+")")
            setResult(-1,data)
            finish()
        }


    }

}
